package pl.mdziedzic.unittest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.internal.selenesedriver.FindElements;
import org.openqa.selenium.internal.seleniumemulation.WaitForPageToLoad;
import org.openqa.selenium.support.ui.Sleeper;

	public class JunitTestCase {
		private static WebDriver driver;
		//private String baseUrl="http://code.google.com/p/selenium/";
		private static StringBuffer verificationErrors = new StringBuffer();

		
		@BeforeClass
		public static void setUp() throws Exception {
			driver = new FirefoxDriver();
			
			//driver = new HtmlUnitDriver();
			driver.manage().deleteAllCookies();
			driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
			driver.get("http://code.google.com/p/selenium/");
			
		}

		@Test
		//klikanie po elementach menu //*[@id="mt"]
		public void clickOverMenu(){
			List<WebElement> menu =  driver.findElements(By.xpath("//*[@id='mt']/a"));

			System.out.println(menu.size());

			for (int i = 1; i < menu.size(); i++) {
				click(By.xpath("//*[@id='mt']/a"+"["+i+"]"));
			}

		}
//		@Test
//		public void testDownloads() throws Exception {
//
//			click(By.linkText("Downloads"));
//			clear(By.id("searchq"));
//			sendKeys(By.id("searchq"),"q");
//			click(By.cssSelector("span.inIssueList > form > input[type=\"submit\"]"));
//
//		}
//
//
//	
//		
//		@Test
//		public void testWiki() throws Exception {
//			click(By.linkText("Wiki"));
//			//driver.findElement().click();
//			assertTrue(isElementPresent(By.id("can")));
//			
//			assertEquals("IPhoneDriver", driver.findElement(By.linkText("IPhoneDriver")).getText());
//		}
//	
//
//		@Test
//		public void testIssues() throws Exception {
//			click(By.linkText("Issues"));
//			
//			assertTrue(isElementPresent(By.linkText("New issue")));
//			try {
//				assertTrue(isElementPresent(By.linkText("Grid")));
//			} catch (Error e) {
//				verificationErrors.append(e.toString());
//			}
//				
//		}	
//		
//		@Test
//		public void testSource() throws Exception {
//			click(By.linkText("Source"));
//			clear(By.name("q"));
//			sendKeys(By.name("q"),"test");
//				
//		}	
//		
//		@Test
//		public void testProjectSearch() throws Exception {
//			click(By.name("projectsearch"));
//			assertTextPresent("Results 1 - 10 of");
//			//, driver.findElement(By.xpath("//div[@id='maincol']/table/tbody/tr/td[2]")).getText());
//			
//		}	
		
//		@Test
//		public void testJunitTestCase() throws Exception {
//
//	
//
//		}

		@AfterClass
		public static void tearDown() throws Exception {
			driver.quit();
			String verificationErrorString = verificationErrors.toString();
			if (!"".equals(verificationErrorString)) {
				fail(verificationErrorString);
			}
		}

		private void assertTextPresent(String text){
			try {
				assertTrue(driver.getPageSource().contains(text));
			} catch (Exception e) {
				
				verificationErrors.append(e.toString());
//				e.printStackTrace();
			}
		}
		private boolean isElementPresent(By by) {
			try {
				driver.findElement(by);
				return true;
			} catch (NoSuchElementException e) {
				return false;
			}
		}
//Implememnted additional methods - that should make code easier to read
		
		private void click(By identifier) {
			driver.findElement(identifier).click();
		}
		private void sendKeys(By identifier, String text) {
			driver.findElement(identifier).sendKeys(text);
		}

		private void clear(By identifier) {
			driver.findElement(identifier).clear();
		}

	}


